#include "ChatrecodTask.h"

ChatrecodTask::ChatrecodTask(char *body, int len, int socketfd): BaseTask(body, len, socketfd)
{
}

ChatrecodTask::~ChatrecodTask()
{
}

void ChatrecodTask::work(SharedMemoryIPC *ipc)
{
    HEAD head={0};
    CHATMSG_REQ chatmsg_req={0};
    CHATMSG_RESP chatmsg_resp={0};
    char send_buffer[1024]={0};

    
    memcpy(&chatmsg_req,m_body,sizeof(chatmsg_req));

   UserModel model;
   vector<CHAT_T> chatmsg_list = model.query_all_chat(chatmsg_req.send_id,chatmsg_req.recv_id);
   cout<<"查询消息列表:"<<chatmsg_list.size()<<endl;

   string all_msgs;
   for(int i=0;i<chatmsg_list.size();i++)
   {
    //    memcpy(chatmsg_resp.msgs+i*sizeof(chatmsg_list[i].masg),chatmsg_list[i].masg,sizeof(chatmsg_list[i].masg));
        all_msgs+=chatmsg_list[i].masg;
   }
   strncpy(chatmsg_resp.msgs,all_msgs.c_str(),sizeof(chatmsg_resp.msgs)-1);
   chatmsg_resp.msgs[sizeof(chatmsg_resp.msgs)-1]='\0';//确保终止
   cout<<"消息响应:"<<chatmsg_resp.msgs<<endl;

    head.service_type = CHATMSG;
    head.data_len = sizeof(chatmsg_resp);

    memcpy(send_buffer,&m_socketfd,sizeof(m_socketfd));
    memcpy(send_buffer+sizeof(m_socketfd),&head,sizeof(head));
    memcpy(send_buffer+sizeof(m_socketfd)+sizeof(head),&chatmsg_resp,sizeof(chatmsg_resp));

    ipc->write_shm(send_buffer);
}
